package com.yilin.medical.activity;

import java.lang.reflect.Type;
import java.util.Map;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.yilin.medical.R;
import com.yilin.medical.http.AsyTask;
import com.yilin.medical.lsh.BaseResult;
import com.yilin.medical.lsh.Http;
import com.yilin.medical.lsh.Post5_2;
import com.yilin.medical.lsh.UserInfo;
import com.yilin.medical.utils.Inject;
import com.yilin.medical.utils.InjectUtils;
import com.yilin.medical.utils.JxShareUtils;
import com.yilin.medical.utils.PromptManager;

/**
 * 登录
 * 
 * @author lijianqun
 * 
 */

public class LoginActivity extends BaseActivity {

	@Inject(R.id.tv_login)
	private TextView login;

	@Inject(R.id.et_phone)
	private EditText number;
	@Inject(R.id.et_password)
	private EditText password;

	@Inject(R.id.cb_autologin)
	private CheckBox rember;

	@Inject(R.id.tv_forgetpassword)
	private TextView forgotPsw;
	JxShareUtils utils;
	private final String REMBER_PASSWORD = "is_rember_password";
	private final String USERNAME = "username";
	private final String PASSWORD = "password";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewWithTitle(R.layout.activity_login);
		InjectUtils.inject(this);
		utils = new JxShareUtils(this);
		initView();
		registerLintener();
	}

	private void initView() {
		mTitle.getTitle().setText("登录");
		mTitle.setTitleRight("注册", View.VISIBLE, this);
		mTitle.getTitleTv().setOnClickListener(this);
		boolean b = getBoolean(REMBER_PASSWORD);
		rember.setChecked(b);
		if (b) {
			password.setText(getString(PASSWORD));
			number.setText(getString(USERNAME));
		}
	}

	private void registerLintener() {
		login.setOnClickListener(this);
		forgotPsw.setOnClickListener(this);
		rember.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				storeValue(REMBER_PASSWORD, isChecked);
				storePswname();
			}
		});
	}

	@Override
	public void onClick(View v) {

		super.onClick(v);
		switch (v.getId()) {
		case R.id.title_tv:
			// TODO 进入注册界面
			forgetPswReg(true);
			break;
		case R.id.tv_login:
			// TODO 走登录接口
			Log.i(this.getClass().getSimpleName(), "login");
			login();
			break;
		case R.id.tv_forgetpassword:
			// TODOｗ　忘记密码
			forgetPswReg(false);
			break;

		}
	}

	// 忘记密码 注册
	private void forgetPswReg(boolean flag) {
		Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
		intent.putExtra(RegisterActivity.REG, flag);
		startActivity(intent);
	}

	// 登录接口
	@SuppressWarnings("unchecked")
	private void login() {
		Post5_2 post = new Post5_2();
		post.setUsername(number.getText().toString().trim());
		post.setPassword(password.getText().toString().trim());
		if (getBoolean(REMBER_PASSWORD)) {
			post.setIsRemember(1);
		} else {
			post.setIsRemember(0);
		}
		Map<String, Object> params = post.toMap(post);

		Type type = new TypeToken<BaseResult<UserInfo>>() {
		}.getType();
		new AsyTask<BaseResult<UserInfo>>(LoginActivity.this,
				Http.HttpApis.login, type, true) {

			@Override
			protected void onPost(BaseResult<UserInfo> result) {
				// TODO Auto-generated method stub
				removeProgressDialog();
				if (result.getSimpleException() != null) {
					PromptManager.showToast(LoginActivity.this, result
							.getSimpleException().getMessage());
				} else {

					if (result.getCode() != 0) {
						PromptManager.showToast(LoginActivity.this,
								result.getText());
					} else {
						startAct(HomeActivity.class, true);
					}
				}
			}
		}.execute(params);

	}

	private void storeValue(String key, Object value) {
		Editor edit = sp.edit();
		if (value instanceof Boolean) {
			edit.putBoolean(key, (Boolean) value);
		}
		if (value instanceof String) {
			edit.putString(key, (String) value);
		}
		edit.commit();
	}

	private boolean getBoolean(String key) {
		return sp.getBoolean(key, false);
	}

	private String getString(String key) {
		return sp.getString(key, "");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		storePswname();
	}

	private void storePswname() {
		boolean checked = rember.isChecked();
		if (checked) {
			// 保存记录
			storeValue(USERNAME, number.getText().toString().trim());
			storeValue(PASSWORD, password.getText().toString().trim());
		} else {
			// 清除
			storeValue(USERNAME, number.getText().toString().trim());
			storeValue(PASSWORD, password.getText().toString().trim());
		}
	}
}